package com.xinchuang.vo;

import org.gdal.gdal.Dataset;
import org.gdal.gdal.gdal;
import org.gdal.gdalconst.gdalconst;
import org.gdal.osr.CoordinateTransformation;
import org.gdal.osr.SpatialReference;
import org.geotools.coverage.grid.GridCoverage2D;
import org.geotools.coverage.grid.GridCoverageFactory;
import org.geotools.gce.geotiff.GeoTiffReader;
import org.geotools.geometry.Envelope2D;
import org.opengis.referencing.crs.CoordinateReferenceSystem;
import org.opengis.referencing.operation.MathTransform;

import java.io.File;
import java.io.IOException;
import java.math.BigDecimal;

/**
 * TODO
 *
 * @author zkwczx
 * @version 1.0
 * @date 2023/11/30 14:51
 */
public class GdalUtil {

    public static void main(String[] args) {
        // 初始化GDAL
        gdal.AllRegister();
        // 打开TIFF文件
//        Dataset tiffDataset = gdal.Open("D:\\desktop\\项目\\昆明\\geo_new\\20140119.tif");
//
        Dataset dataset = gdal.Open("D:\\desktop\\项目\\昆明\\geo_new\\20140119.tif");
//
        if (dataset != null) {

            // 获取X轴和Y轴的分辨率
            double[] geotransform = dataset.GetGeoTransform();
            double xResolution = geotransform[1];
            double yResolution = geotransform[5];

            // 获取地理参考坐标系
            SpatialReference spatialRef = new SpatialReference(dataset.GetProjectionRef());

            BigDecimal x = new BigDecimal(xResolution);
            BigDecimal y = new BigDecimal(yResolution);

            // 打印分辨率和单位
            System.out.println("X Resolution: " + x.setScale(8, BigDecimal.ROUND_HALF_UP).doubleValue() + " meters");
            System.out.println("Y Resolution: " + y.setScale(8, BigDecimal.ROUND_HALF_UP).doubleValue() + " meters");
            System.out.println("Unit: " + spatialRef.GetLinearUnitsName());
        } else {
            System.out.println("Failed to open the TIFF file.");
        }

        // 关闭TIFF文件
        dataset.delete();
//
//        if (tiffDataset != null) {
//            // 获取分辨率
//            double[] geotransform = tiffDataset.GetGeoTransform();
//            double pixelSizeX = geotransform[1];
//            double pixelSizeY = geotransform[5];
//            System.out.println("X轴分辨率: " + pixelSizeX + " 米");
//            System.out.println("Y轴分辨率: " + pixelSizeY + " 米");
//            // 关闭数据集
//            tiffDataset.delete();
//        } else {
//            System.out.println("无法打开TIFF文件");
//        }
//        // 清理GDAL环境
//        gdal.GDALDestroyDriverManager();

        // ---------------------------------
//        double xResolution, yResolution;
//
//        // 初始化GDAL
//        gdal.AllRegister();
//
//        String filePath =  "D:\\desktop\\项目\\昆明\\geo_new\\20140119.tif";
//        // 打开TIFF文件
//        Dataset dataset = gdal.Open(filePath, gdalconst.GA_ReadOnly);
//
//        if (dataset == null) {
//            System.out.println("无法打开TIFF文件");
//            return;
//        }

        // 获取X轴和Y轴的分辨率
//        xResolution = dataset.GetGeoTransform()[1];
//        yResolution = -dataset.GetGeoTransform()[5];

        // 获取投影信息
//        SpatialReference source = new SpatialReference(dataset.GetProjection());
//        SpatialReference target = new SpatialReference();
        // 设置目标投影为WGS84坐标系
//        target.ImportFromEPSG(4326);
//        CoordinateTransformation transform = new CoordinateTransformation(source, target);

        // 转换分辨率单位为米
//        double[] xy = { xResolution, yResolution };
////        transform.TransformPoint(xy);
//        xResolution = Math.abs(xy[0]);
//        yResolution = Math.abs(xy[1]);
//
//        System.out.println("X轴分辨率: " + xResolution + " 米");
//        System.out.println("Y轴分辨率: " + yResolution + " 米");
//
//        // 释放资源
//        dataset.delete();
//        gdal.GDALDestroyDriverManager();

    }

}
